package com.sinodata.bsm.cicp.finder.hp;

/**
 * HP操作系统 Cpu的发现程序
          多CPU情形:
    #sar -M -u 1 1
    HP-UX bj_hp2 B.11.00 U 9000/800    12/29/03
    12:52:58     cpu    %usr    %sys    %wio   %idle
    12:52:59       0       1       3      11      85
                   1      21       8       2      68
                   2       2       4       0      93
                   3       1       1       2      95
              system       7       4       4      85
    HP,单CPU情形:
    #sar -M -u 1 1
    HP-UX bj_hp2 B.11.00 U 9000/800    12/29/03
    12:52:58     cpu    %usr    %sys    %wio   %idle
    12:52:59       0       1       3      11      85
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

public class Cpu extends HPFinder {

    private static final Logger logger = Logger.getLogger(Cpu.class);
    private static final String SHELL = "sar -M -u 1 1";//可以获得cpuid
    private static final String CMDSTR = "machinfo";//可以得到cpu类型和cpu主频

    public static void main(String[] args) {
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] cpuRes = null;
        BufferedReader reader = null;
        String strLine = null;
        String type = "N/A";
        String frequency = "N/A";
        try {
            reader = CommandReader.getReader(ip, CMDSTR, 0, 0);
            //执行"machinfo"的输出格式: 2 Intel(R) Itanium 2 9100 series processors (1.67 GHz, 18 MB)
            String cpuTypePtn = "\\s+\\d+\\s+(.*)processors\\s+\\((.*),.*";
            Pattern pattern = Pattern.compile(cpuTypePtn);
            while ((strLine = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    type = matcher.group(1);
                    frequency = matcher.group(2);
                }
            }
            reader = CommandReader.getReader(ip, SHELL, 0, 0);
            String str = null;
            String strTmp = null;
            StringTokenizer st = null;
            while ((str = reader.readLine()) != null) {
                st = new StringTokenizer(str);
                strTmp = st.nextToken();
                if (strTmp.indexOf(":") != -1) {
                    strTmp = st.nextToken();
                }
                if (strTmp.indexOf("-") != -1) {
                    continue;
                }
                try {
                    Integer.parseInt(strTmp);
                    cpuRes = new String[3];
                    list.add(cpuRes);
                    cpuRes[0] = strTmp;
                    cpuRes[1] = "CPU" + strTmp;
                    cpuRes[2] = getHostName(ip) + "CPU" + strTmp;
                    cpuRes[3] = frequency;//CPU主频
                    cpuRes[4] = strTmp; //CPU ID
                    cpuRes[5] = type;//CPU类型
                } catch (Exception e) {
                    logger.error("failed to format number", e);
                }
            }
            return list;
        } catch (IOException e) {
            logger.error("failed to find PH cpu", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return null;
    }
}
